<?php
/**
 * Created by PhpStorm.
 * User: bevan
 * Date: 2018/1/3
 * Time: 下午7:28
 */

namespace app\admin\controller;

use app\admin\facade\Group;
use think\facade\Request;
use app\common\controller\Admin;

/**
 * 权限组操作
 * Class GroupController
 * @package app\admin\controller
 */
class GroupController extends Admin
{
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * @title 获取所有的权限组
     * @desc  {"0":"接口地址：","1":"请求方式：GET","2":"接口备注："}
     * @return {"name":"code","type":"json","required":true,"desc":"状态码","level":"1"}
     * @return {"name":"msg","type":"json","required":true,"desc":"状态信息","level":"1"}
     * @return {"name":"data","type":"json","required":true,"desc":"返回的数据","level":"1"}
     */
    public function index()
    {
        $keyword = Request::has('keyword')?Request::param('keyword'):'';
        $this->results(Group::allList($keyword));
    }

    /**
     * @title 新增权限组
     * @desc  {"0":"接口地址：","1":"请求方式：GET","2":"接口备注："}
     * @return {"name":"code","type":"json","required":true,"desc":"状态码","level":"1"}
     * @return {"name":"msg","type":"json","required":true,"desc":"状态信息","level":"1"}
     * @return {"name":"data","type":"json","required":true,"desc":"返回的数据","level":"1"}
     */
    public function save()
    {
        //数据验证
        $result = $this->validate(Request::param(),
            'app\admin\validate\GroupValidate');
        if (true !== $result) {
            return json(['code' => 4001, 'msg' => $result, 'data' => []]);
        }

        $this->results(Group::save(Request::param()));
    }

    /**
     *
     * @method  编辑权限组信息
     * @version
     * @User: bevan
     * @param $id
     * @return
     */
    public function read($id)
    {
        cmf_check_primary_key_is_number($id);

        $this->results(Group::read($id));
    }

    /**
     *
     * @method 更新一个权限组
     * @version
     * @User: bevan
     * @param $id  主键
     * @return
     */
    public function update($id)
    {
        cmf_check_primary_key_is_number($id);

        //数据验证
        $result = $this->validate(Request::param(),
            'app\admin\validate\GroupValidate');
        if (true !== $result) {
            return json(['code' => 4001, 'msg' => $result, 'data' => []]);
        }
        $this->results(Group::update($id, Request::param()));
    }

    /**
     *
     * @method  删除一个权限组
     * @version
     * @User: bevan
     * @param $id 主键
     * @return
     */
    public function delete($id)
    {
        cmf_check_primary_key_is_number($id);

        if($id == 1){
            return ['code'=>4001,'msg'=>'不允许删除超级管理员组','data'=>[]];
        }

        $this->results(Group::delete($id));
    }

    /**
     *
     * @method 权限设置信息获取
     * @version
     * @User: bevan
     * @return
     */
    public function getAuth($id = 0)
    {
        cmf_check_primary_key_is_number($id);

        $this->results(Group::getAuth($id));
    }



    /**
     *
     * @method 切换组状态
     * @version
     * @User: bevan
     * @param $id
     * @return
     */
    public function toggleGroupStatus($id)
    {
        cmf_check_primary_key_is_number($id);

        $this->results(Group::toggleGroupStatus($id));
    }

}